package com.dd.common.utils;

import com.dd.common.config.GlobalConfig;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;

/**
 * 字符串生成工具类
 */
public class StrGenerator {
    private static final Random RANDOM = new Random();

    // 头像可选风格
    private static final String[] STYLES = {
            "lorelei", "thumbs", "pixel-art", "adventurer", "adventurer-neutral",
            "big-smile", "bottts-neutral", "fun-emoji", "micah", "notionists"
    };


    /**
     * 制作UUID
     *
     * @return String
     */
    public static String makeUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
    }

    /**
     * 制作MD5
     *
     * @param data 需加密的数据
     * @return String
     */
    public static String makeMd5(String data) {
        if (data == null || data.isEmpty()) {
            return "";
        }
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] digest = md5.digest(data.getBytes(StandardCharsets.UTF_8));
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(Integer.toHexString((b & 0xFF) | 0x100), 1, 3);
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm not found", e);
        }
    }

    /**
     * 生成唯一Token
     *
     * @return String
     */
    public static String makeToken() {
        // 使用更安全的随机源（如 SecureRandom）
        String randomPart = randomString(16);
        long timestamp = System.currentTimeMillis();
        String secret = Optional.ofNullable(GlobalConfig.secret)
                .orElse("ikun");

        // 构造唯一输入
        String input = makeUUID() + timestamp + randomPart;

        // 加盐哈希
        return makeMd5(input + secret);
    }

    /**
     * 返回随机字符串
     *
     * @param length 要生成的长度
     * @return String
     */
    public static String randomString(int length) {
        if (length <= 0) return "";
        String chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(chars.charAt(RANDOM.nextInt(chars.length())));
        }
        return sb.toString();
    }

    /**
     * 返回随机数字字符串
     *
     * @param length 要生成的长度
     * @return String
     */
    public static String randomInt(int length) {
        if (length <= 0) return "";
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(RANDOM.nextInt(10));
        }
        return sb.toString();
    }


    /**
     * 随机生成一个 DiceBear 风格头像 URL
     *
     * @return 返回一个随机的头像 SVG URL
     */
    public static String generateRandomAvatar() {
        String style = STYLES[RANDOM.nextInt(STYLES.length)];
        String seed = randomString(4); // 生成4位随机seed
        return "https://api.dicebear.com/9.x/" + style + "/svg?seed=" + seed;
    }
}
